package com.huanwei.login_regist;

import android.content.Intent;
import android.os.Bundle;
import android.support.v7.app.AppCompatActivity;
import android.text.TextUtils;
import android.view.View;
import android.widget.Button;
import android.widget.EditText;
import android.widget.Toast;


import com.huanwei.cc.R;
import com.huanwei.mainfragment.MainActivity;
import com.huanwei.cc.db.Z_Login_UserInfoDao;
import com.huanwei.cc.utils.SpfUtils;

import butterknife.BindView;
import butterknife.ButterKnife;
import butterknife.OnClick;

/**
 * 登录页面，
 */
public class LoginActivity extends AppCompatActivity {

    @BindView(R.id.login_phone)
    EditText loginPhone;
    @BindView(R.id.login_mm)
    EditText loginMm;
    @BindView(R.id.but_login)
    Button butLogin;
    @BindView(R.id.but_regist)
    Button butRegist;
    Z_Login_UserInfoDao z_userInfoDao;

    @Override
    protected void onCreate(Bundle savedInstanceState) {
        super.onCreate(savedInstanceState);
        setContentView(R.layout.activity_login);
        ButterKnife.bind(this);
        z_userInfoDao = new Z_Login_UserInfoDao(this);
    }

    @OnClick({R.id.img_back, R.id.but_login, R.id.but_regist, R.id.but_youke})
    public void onViewClicked(View view) {
        switch (view.getId()) {
            case R.id.img_back:
                finish();
                break;

            case R.id.but_youke:
                startActivity(new Intent(this, MainActivity.class));
                SpfUtils.put(this, "type", "1");
                finish();
                break;
            case R.id.but_login:
                if (TextUtils.isEmpty(loginPhone.getText().toString())) {
                    Toast.makeText(this, "请输入账号", Toast.LENGTH_SHORT).show();
                    return;
                } else if (TextUtils.isEmpty(loginMm.getText().toString())) {
                    Toast.makeText(this, "请密码", Toast.LENGTH_SHORT).show();
                    return;
                }
                if (loginPhone.getText().toString().equals("admin") && loginMm.getText().toString().equals("admin")) {

                    startActivity(new Intent(this, Main_LS_Activity.class));
                    finish();
                    return;
                }

                if (z_userInfoDao.shifoucunzai(loginPhone.getText().toString())) {
                    if (!z_userInfoDao.getpassword(loginPhone.getText().toString()).equals(loginMm.getText().toString())) {
                        Toast.makeText(this, "密码错误！", Toast.LENGTH_SHORT).show();
                    } else {
                        Toast.makeText(this, "登录成功！", Toast.LENGTH_SHORT).show();
                        startActivity(new Intent(this, MainActivity.class));
                        SpfUtils.put(this, "islogin", true);
                        SpfUtils.put(this, "type", "2");
                        SpfUtils.put(this, "name", z_userInfoDao.getname(loginPhone.getText().toString()));
                        finish();
                    }

                } else {
                    Toast.makeText(this, "账号不存在！", Toast.LENGTH_SHORT).show();
                }
                break;
            case R.id.but_regist:
                startActivity(new Intent(this, RegistActivity.class));
                break;
        }
    }
}
